package com.harmonyos.cases.utils;

import com.intellij.ui.jcef.JBCefBrowser;
import org.cef.browser.CefBrowser;
import org.cef.browser.CefFrame;
import org.cef.handler.CefLoadHandlerAdapter;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

// 数据统计类
public class StatisticsUtils {
    // 执行百度统计
    public static void loadBaiduTongjiScript(JBCefBrowser browser) {

        String javascriptEventTrackingCode = "javascript:_hmt.push(['_trackEvent', 'Plugin', 'Button_Click', 'MyButton']);";

        try {
            InputStream inputStream = StatisticsUtils.class.getResourceAsStream("/baidu_tongji.js");
            if (inputStream != null) {
                byte[] buffer = inputStream.readAllBytes();
                String scriptContent = new String(buffer, StandardCharsets.UTF_8);
                browser.getJBCefClient().addLoadHandler(new CefLoadHandlerAdapter() {
                    @Override
                    public void onLoadEnd(CefBrowser browser, CefFrame frame, int httpStatusCode) {
                        super.onLoadEnd(browser, frame, httpStatusCode);
                        System.out.println("111111111  " + browser.getURL());
                        browser.executeJavaScript(scriptContent, browser.getURL(), 0);
//                        browser.executeJavaScript(javascriptEventTrackingCode, "", 0);
                    }
                }, browser.getCefBrowser());
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}
